<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>医院管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">医院名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="areaName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        <!-- 添加/编辑弹窗 -->
        <script type="text/html" id="areaEditForm">
            <form class="layui-form" style="padding: 20px;" lay-filter="areaForm">
                <input type="hidden" name="areaId">
                <div class="layui-form-item">
                    <label class="layui-form-label">院区名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="areaName" placeholder="请输入院区名称" class="layui-input" lay-verify="required">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">院区地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="areaAddress" placeholder="请输入院区地址" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">院区电话</label>
                    <div class="layui-input-block">
                        <input type="text" name="areaPhone" placeholder="请输入院区电话" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">创建时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="areaStartTime" placeholder="请选择创建时间" class="layui-input" id="startTime">
                    </div>
                </div>
                <div class="layui-form-item " id="lay_areaStatus" >
                    <label class="layui-form-label">院区状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="areaStatus" value="1" title="正常" checked>
                        <input type="radio" name="areaStatus" value="0" title="禁用">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="areaRemark" placeholder="请输入备注" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="saveArea">保存</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate,
            layer=layui.layer;


        // 初始化表格
        table.render({
            elem: '#currentTableId',
            url: '/area/page', // 后端查询接口
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            // //后端响应的数据结构不是前端想要的  所有要进行转换
            // parseData: function(res){
            //     return {
            //         "code": res.code,
            //         "msg": res.msg,
            //         "count": res.data.total,
            //         "data": res.data.list
            //     }
            // },
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'areaId', width: 80, title: 'ID', sort: true},
                {field: 'areaName', width: 150, title: '院区名称'},
                {field: 'areaAddress', width: 200, title: '院区地址'},
                {field: 'areaPhone', width: 150, title: '院区电话'},
                {field: 'areaStartTime', width: 120, title: '创建时间'},
                {field: 'areaStatus', width: 100, title: '状态', templet: function(d){
                        return d.areaStatus == 1 ? '<span style="color: green;">正常</span>' : '<span style="color: red;">禁用</span>';
                    }},
                {field: 'areaRemark', title: '备注'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // 执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: {
                    areaName: data.field.areaName
                }
            });
            return false;
        });

        // toolbar监听事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                openAreaEditDialog(null, '添加院区');

            }
        });

        // 监听表格工具栏事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                openAreaEditDialog(data, '编辑院区');
            } else if (obj.event === 'delete') {
                deleteArea(data.areaId, obj);
            }
        });
        var index;
        // 打开添加/编辑对话框
        function openAreaEditDialog(data, title) {

            index= layer.open({
                title: title,
                type: 1,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['600px', '600px'],
                content: $('#areaEditForm').html(),
                success: function(layero, index) {
                    // 初始化日期控件
                    laydate.render({
                        elem: '#startTime',
                        theme: '#448aff',
                        calendar: true
                    });
                    if (!data) {
                        // 新增模式 - 隐藏状态字段
                        $("#lay_areaStatus").hide();
                    } else {
                        // 编辑模式 - 显示状态字段
                        $("#lay_areaStatus").show();
                    }

                    // 如果是编辑模式，填充表单数据
                    if (data) {
                        form.val("areaForm", data);
                    }

                    // 监听表单提交
                    form.on('submit(saveArea)', function(formdata) {
                        saveArea(formdata.field);
                        return false;
                    });
                }
            });
        }

        // 保存院区信息
        function saveArea(data) {
            var url = data.areaId ? '/area/update' : '/area/add';
            var method = data.areaId ? 'PUT' : 'POST';
            $.ajax({
                url: url,
                type: method,
                contentType: 'application/json',
                data: JSON.stringify(data),
                success: function(res) {
                    if(res.code == 0) {
                        layer.msg(res.msg, {icon: 1});
                        layer.close(index);
                        table.reload('currentTableId');
                    }else{
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function() {
                    layer.msg('操作失败', {icon: 2});
                }
            });
        }

        // 删除院区
        function deleteArea(areaId, obj) {
            layer.confirm('确定要删除这个院区吗？', function(index) {
                $.ajax({
                    url: '/area/delete/' + areaId,
                    type: 'DELETE',
                    success: function(res) {
                        if(res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);
                            table.reload('currentTableId');
                        }else{
                            layer.msg(res.msg, {icon: 2});
                        }
                    },
                    error: function() {
                        layer.msg('删除失败', {icon: 2});
                    }
                });
                layer.close(index);
            });
        }

    });
</script>

</body>
</html>
